package com.derek.omshopping.ui.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.derek.omshopping.R;
import com.derek.omshopping.bean.home.HomeMainTypeBean;
import com.derek.omshopping.config.UrlConfig;
import com.derek.omshopping.ui.home.view.DetailsPageActivty;
import com.derek.omshopping.ui.home.view.OnItemClickListener;

import java.util.List;

/**
 * Created by duyb on 2016-10-05.
 * Desc:首页轮播图下面button
 */

public class HomeButtonAdapter extends RecyclerView.Adapter<HomeButtonAdapter.MyButtonViewHolder> {
    private List<HomeMainTypeBean.SetListBean> list;
    private Context context;
    private OnItemClickListener listener;//点击事件监听 后面holder.itemVIew单击事件用到

    //listner 的set方法 adapter调用
    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public HomeButtonAdapter(Context context, List<HomeMainTypeBean.SetListBean> list) {
        this.context = context;
        this.list = list;
        //Log.e("buton__>",list.size()+"");
    }

    @Override
    public MyButtonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyButtonViewHolder holder = new MyButtonViewHolder(LayoutInflater
                .from(context).inflate(R.layout.item_radio_recyclerview, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyButtonViewHolder holder, final int position) {
        holder.tv.setText(list.get(position).getContentList().get(0).getAltDesc());
        Glide.with(context).load(Uri.parse(UrlConfig.PRE_BASEURL + list.get(position).getContentList().get(0).getContsPathNm() + list.get(position).getContentList().get(0).getContsFileNm())).into(holder.simpleDraweeView);
        holder.simpleDraweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, DetailsPageActivty.class);
                String imgId=list.get(position).getContentList().get(0).getConnectTgtAddr();
                intent.putExtra("imgId",imgId);
                context.startActivity(intent);
            }
        });

        // holder.simpleDraweeView.setImageURI(Uri.parse(list.get(position).getIcon()));

        /*if (list != null) {
            //view点击事件为了动态得到itemview以及其位置的position
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    //接口回调实现监听
                    listener.onItemClick(holder.itemView, pos);
                }
            });
        }*/
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyButtonViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        ImageView simpleDraweeView;

        public MyButtonViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.textView_button_title);
            simpleDraweeView = (ImageView) itemView.findViewById(R.id.simpleView_button_icon);
        }
    }
}
